package com.ruoyi.procurementmanagement.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.procurementmanagement.mapper.EsCategoryMapper;
import com.ruoyi.procurementmanagement.domain.EsCategory;
import com.ruoyi.procurementmanagement.service.IEsCategoryService;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-14
 */
@Service
public class EsCategoryServiceImpl implements IEsCategoryService
{
    @Autowired
    private EsCategoryMapper esCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param categoryId 商品分类主键
     * @return 商品分类
     */
    @Override
    public EsCategory selectEsCategoryByCategoryId(Long categoryId)
    {
        return esCategoryMapper.selectEsCategoryByCategoryId(categoryId);
    }

    /**
     * 查询商品分类列表
     *
     * @param esCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<EsCategory> selectEsCategoryList(EsCategory esCategory)
    {
        return esCategoryMapper.selectEsCategoryList(esCategory);
    }

    /**
     * 新增商品分类
     *
     * @param esCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertEsCategory(EsCategory esCategory)
    {
        return esCategoryMapper.insertEsCategory(esCategory);
    }

    /**
     * 修改商品分类
     *
     * @param esCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateEsCategory(EsCategory esCategory)
    {
        return esCategoryMapper.updateEsCategory(esCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param categoryIds 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteEsCategoryByCategoryIds(Long[] categoryIds)
    {
        return esCategoryMapper.deleteEsCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除商品分类信息
     *
     * @param categoryId 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteEsCategoryByCategoryId(Long categoryId)
    {
        return esCategoryMapper.deleteEsCategoryByCategoryId(categoryId);
    }

    @Override
    public List<Long> getAllChildCategoryIds(Long categoryId) {
        List<Long> result = new ArrayList<>();
        result.add(categoryId); // 包含自身

        // 递归获取所有子分类ID
        List<Long> children = esCategoryMapper.selectChildrenCategoryIds(categoryId);
        for (Long childId : children) {
            result.addAll(getAllChildCategoryIds(childId));
        }

        return result;
    }
}
